export default {
  data() {
    return {
      cachePage: {
        pageSize: 10,
        pageNum: 1
      },
      tableData: [],
      total: 0
    }
  },
  methods: {
    search(data, pageObj, api) {
      if (!data) {
        data = this.cacheData
      }
      if (!pageObj) {
        pageObj = {
          pageSize: 10,
          pageNum: 1
        }
        this.$refs.table && this.$refs.table.resetPage()
      } else {
        this.$refs.table && this.$refs.table.setCurrPage(pageObj.pageNum)
      }
      this.cacheData = data
      this.cachePage = pageObj
      const params = {
        ...data,
        ...pageObj
      }
      if (!this.hidetableload) {
        this.$store.dispatch("setTableLoading", true)
      }

      return this.$ajax
        .get(api, params)
        .then((res) => {
          this.tableData = res.data.list || res.data
          this.total = res.data.total
          if (this.databack) {
            this.databack()
          }
          this.$nextTick(() => {
            this.$store.dispatch("setTableLoading", false)
            // 切换表格分页时将滚动条滚动到顶部 #32321
            const tableDom = document.getElementsByClassName("el-table__body-wrapper")
            for (let i = 0; i < tableDom.length; i++) {
              tableDom[i].scrollTo(0, 0)
            }
          })
          return Promise.resolve(res)
        })
        .catch(() => {
          this.tableData = []
          this.total = 0
          this.$nextTick(() => {
            this.$store.dispatch("setTableLoading", false)
          })
        })
    },
    handleTableChange(data) {
      this.search(null, data, this.queryApi)
    }
  }
}
